﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using WeiBeeCommon.Core;
using Microsoft.Win32;

namespace QunFaExpert
{
    /// <summary>
    /// Interaction logic for Twitters.xaml
    /// </summary>
    public partial class Twitters : Window
    {
        public Twitters()
        {
            InitializeComponent();
        }

        private void btSend_Click(object sender, RoutedEventArgs e)
        {
            twittertxt = tbText.Text;
            tbText.Text = string.Empty;
            bgwBatchUpdater.RunWorkerAsync();
        }

        private string picturefile = string.Empty;
        private string twittertxt;
        private readonly BackgroundWorker bgwBatchUpdater = new BackgroundWorker();
        private List<IWeiBee> weibees = new List<IWeiBee>();
        private int currentWeiBee = 0;
        private int successCount = 0;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var entities = new EntityHelper().LoadEntities();
            foreach (var entity in entities)
            {
                weibees.Add(entity.GetWeiBee());
            }
            bgwBatchUpdater.DoWork += BgwBatchUpdater_DoWork;
            bgwBatchUpdater.RunWorkerCompleted += BgwBatchUpdater_RunWorkerCompleted;
        }

        void BgwBatchUpdater_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(picturefile))
                {
                    weibees[currentWeiBee].AddTwitter(twittertxt);
                }
                else
                {
                    weibees[currentWeiBee].AddPicture(twittertxt, picturefile);
                }
                successCount++;
            }
            catch
            {
                
            }
        }

        private OnlineLicense license = new OnlineLicense();
        void BgwBatchUpdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            currentWeiBee++;
            if (currentWeiBee < weibees.Count)
            {
                bgwBatchUpdater.RunWorkerAsync();
            }
            if (currentWeiBee == weibees.Count)
            {
                for (int i = 0; i < successCount * 10 / 23; i++)
                {
                    license.GetLeftSofaAmount();
                }
                MessageBox.Show(string.Format("微博发送成功，共发送{0}条", successCount));
                twittertxt = string.Empty;
                picturefile = string.Empty;
                currentWeiBee = 0;
                successCount = 0;
            }
        }

        private void btPic_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "picture"; // Default file name
            dlg.DefaultExt = "*.*"; // Default file extension
            
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                picturefile = dlg.FileName;
            }
        }
    }
}
